//
//  LBCDetailViewController.m
//  Moment
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCDetailViewController.h"

@interface LBCDetailViewController ()<UIWebViewDelegate>

@property (nonatomic, assign)NSInteger i;

@property (nonatomic,retain)UIWebView *webView;

@end

@implementation LBCDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleStr;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_back_h@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftButton)];
//    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightBtn.frame = CGRectMake(270, 0, self.navigationController.navigationBar.frame.size.height, self.navigationController.navigationBar.frame.size.height);
//    [self.rightView addSubview:rightBtn];
//    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.backButton.frame.size.width / 4, self.backButton.frame.size.width / 4, self.backButton.frame.size.width / 2, self.backButton.frame.size.width / 2)];
//    rightImageView.image = [UIImage imageNamed:@"iconfont-gengduo-2.png"];
//    [rightBtn addSubview:rightImageView];
//    [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100)];
    _webView.backgroundColor = [UIColor clearColor];
    [(UIScrollView *)[[_webView subviews] objectAtIndex:0] setBounces:NO];

    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSString *bodyStr = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&version=3.0.6", self.contentId];
    [AFNetworkTool postUrl:urlStr body:bodyStr result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        [_webView loadHTMLString:[dic objectForKey:@"html"] baseURL:nil];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        [self.view addSubview:_webView];
    } failure:^(NSError *error) {
    
        
    }];
    
}

#pragma mark --  webView自适应
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isNight"] isEqualToString:@"YES"]) {
        //        self.homeDataTableView.backgroundColor = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1];
        self.view.backgroundColor = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1];
        //字体大小
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
        //字体颜色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
        //页面背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
    }

    NSString *str1 = [NSString stringWithFormat:@"%0.2f",SCREEN_WIDTH-20];
    //webview 自适应高度
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth= %@;"//缩放系数,调动这个参数可以改变图片的宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "if (oldwidth >= 300) {"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);", str1]];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
    
}

- (void)leftButton{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
